﻿using System;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using System.Collections.Generic;
using Resources;
using System.Text;
using Wicresoft.RDW.Utility;
using System.Linq;

namespace Wicresoft.RDW.Feedback
{
    public partial class FeedbackQuestion : BasePage_BackEnd
    {
        private static bool flag = false;
        private static int questionId;
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            flag = false;
            lbtnStatus(flag);
        }
        public override void InitControlResource()
        {
            lbtnSave.Text = ManageResource.mgr_btnOK;
            lbtnReturn.Text = ManageResource.mgr_btnCancel;
            btnFeedbackQuestion.Text = NameResource.Btn_FeedbackQuestion;

            rfvQuestion_CN.ErrorMessage = NameResource.Question_Question_CN + CommonResource.NotNull;
            rfvQuestion_EN.ErrorMessage = NameResource.Question_Question_EN + CommonResource.NotNull;
            rfvAnswer1_CN.ErrorMessage = NameResource.Answer_Answer_CN + CommonResource.NotNull;
            rfvAnswer2_CN.ErrorMessage = NameResource.Answer_Answer_CN + CommonResource.NotNull;
            rfvAnswer1_EN.ErrorMessage = NameResource.Answer_Answer_EN + CommonResource.NotNull;
            rfvAnswer2_EN.ErrorMessage = NameResource.Answer_Answer_EN + CommonResource.NotNull;

            int i = 0;
            gvFeedbackQuestion.Columns[i++].HeaderText = NameResource.No;
            gvFeedbackQuestion.Columns[i++].HeaderText = NameResource.Question_Question_CN;
            gvFeedbackQuestion.Columns[i++].HeaderText = NameResource.Question_Question_EN;
            gvFeedbackQuestion.Columns[i++].HeaderText = NameResource.Answer_Answer_CN;
            gvFeedbackQuestion.Columns[i++].HeaderText = NameResource.Answer_Answer_EN;
            gvFeedbackQuestion.Columns[i++].HeaderText = ManageResource.mgr_lblAction;

            gvFeedbackQuestion.DataSource = QusetionPresent.GetList(p => p.Status == (int)QuestionStatus.Active);
            gvFeedbackQuestion.DataBind();
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }
        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = QusetionPresent.GetPaged(pageIndex, pager.PageSize, p => p.Status == (int)QuestionStatus.Active, p => p.QuestionId, true);
            gvFeedbackQuestion.DataSource = dataPagerSource;
            gvFeedbackQuestion.DataBind();
            pager.IntialProperties(dataPagerSource.TotalCount);
            lbtnStatus(flag);
        }

        protected void gvFeedbackQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int j = 0;
                e.Row.Cells[j++].Text = (e.Row.RowIndex + 1 + pager.PageSize * pager.CurrentPageIndex).ToString();
                Model.Question question = e.Row.DataItem as Model.Question;
                if (question != null)
                {
                    List<Model.Answer> answerList = AnswerPresent.GetList(p => p.QuestionId == question.QuestionId);
                    StringBuilder answer_CN = new StringBuilder();
                    StringBuilder answer_EN = new StringBuilder();
                    answer_CN.Append(answerList[0].Answer_CN);
                    answer_EN.Append(answerList[0].Answer_EN);
                    for (int i = 1; i < answerList.Count; i++)
                    {
                        answer_CN.Append("#" + answerList[i].Answer_CN);
                        answer_EN.Append("#" + answerList[i].Answer_EN);
                    }
                    j += 2;
                    (e.Row.Cells[j++].FindControl("lblAnswer_CN") as Label).Text = answer_CN.ToString();
                    (e.Row.Cells[j++].FindControl("lblAnswer_EN") as Label).Text = answer_EN.ToString();

                    (e.Row.Cells[j].FindControl("lnkbtnDelete") as LinkButton).Text = ManageResource.mgr_btnDelete;
                    (e.Row.Cells[j].FindControl("lnkbtnDelete") as LinkButton).Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "?');");
                    (e.Row.Cells[j].FindControl("lnkbtnUpdate") as LinkButton).Text = NameResource.Btn_Edit;
                }
            }
        }

        protected void btnFeedbackQuestion_Click(object sender, EventArgs e)
        {
            txtQuestion_CN.Text = string.Empty;
            txtQuestion_EN.Text = string.Empty;
            txtAnswer1_CN.Text = string.Empty;
            txtAnswer1_EN.Text = string.Empty;
            txtAnswer2_CN.Text = string.Empty;
            txtAnswer2_EN.Text = string.Empty;
            txtAnswer3_CN.Text = string.Empty;
            txtAnswer3_EN.Text = string.Empty;
            phQuestion.Visible = true;
            flag = true;
            lbtnStatus(flag);
            questionId = 0;
        }

        protected void gvFeedbackQuestion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            int questionId = (int)gvFeedbackQuestion.DataKeys[i]["QuestionId"];

            var question = QusetionPresent.GetById(questionId);
            question.Status = (int)QuestionStatus.InActive;
            QusetionPresent.Update(question);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void gvFeedbackQuestion_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            questionId = (int)gvFeedbackQuestion.DataKeys[i]["QuestionId"];
            phQuestion.Visible = true;
            flag = true;
            lbtnStatus(flag);
            var question = QusetionPresent.GetById(questionId);
            if (question != null)
            {
                txtQuestion_CN.Text = question.Question_CN;
                txtQuestion_EN.Text = question.Question_EN;
            }
            var answerList = AnswerPresent.GetList(p => p.QuestionId == questionId);
            txtAnswer1_CN.Text = answerList[0].Answer_CN;
            txtAnswer1_EN.Text = answerList[0].Answer_EN;
            txtAnswer2_CN.Text = answerList[1].Answer_CN;
            txtAnswer2_EN.Text = answerList[1].Answer_EN;
            if (answerList.Count > 2)
            {
                txtAnswer3_CN.Text = answerList[2].Answer_CN;
                txtAnswer3_EN.Text = answerList[2].Answer_EN;
            }
            else
            {
                txtAnswer3_CN.Text = string.Empty;
                txtAnswer3_EN.Text = string.Empty;
            }
        }

        protected void lbtnStatus(bool flag)
        {
            if (flag)
            {
                for (int i = 0; i < gvFeedbackQuestion.Rows.Count; i++)
                {
                    LinkButton lbtnDelete = gvFeedbackQuestion.Rows[i].FindControl("lnkbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Add("disabled", "true");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return false;");
                    LinkButton lbtnUpdate = gvFeedbackQuestion.Rows[i].FindControl("lnkbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Add("disabled", "true");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return false;");
                }
            }
            else
            {
                for (int i = 0; i < gvFeedbackQuestion.Rows.Count; i++)
                {
                    LinkButton lbtnDelete = gvFeedbackQuestion.Rows[i].FindControl("lnkbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Remove("disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                    LinkButton lbtnUpdate = gvFeedbackQuestion.Rows[i].FindControl("lnkbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Remove("disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return true;");
                }
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAnswer3_CN.Text) && !string.IsNullOrEmpty(txtAnswer3_EN.Text))
            {
                cvAnswer3_CN.IsValid = false;
                cvAnswer3_CN.ErrorMessage = NameResource.Answer_Answer_CN + CommonResource.NotNull;
            }
            else if (!string.IsNullOrEmpty(txtAnswer3_CN.Text) && string.IsNullOrEmpty(txtAnswer3_EN.Text))
            {
                cvAnswer3_EN.IsValid = false;
                cvAnswer3_EN.ErrorMessage = NameResource.Answer_Answer_EN + CommonResource.NotNull;
            }
            else
            {
                phQuestion.Visible = false;
                flag = false;
                lbtnStatus(flag);
                var question = QusetionPresent.GetById(questionId);
                if (question != null)
                {
                    question.Question_CN = txtQuestion_CN.Text;
                    question.Question_EN = txtQuestion_EN.Text;
                    QusetionPresent.Update(question);
                    var answerList = AnswerPresent.GetList(p => p.QuestionId == questionId);
                    answerList[0].Answer_CN = txtAnswer1_CN.Text;
                    answerList[0].Answer_EN = txtAnswer1_EN.Text;
                    AnswerPresent.Update(answerList[0]);
                    answerList[1].Answer_CN = txtAnswer2_CN.Text;
                    answerList[1].Answer_EN = txtAnswer2_EN.Text;
                    AnswerPresent.Update(answerList[1]);
                    if (answerList.Count > 2)
                    {
                        if (!string.IsNullOrEmpty(txtAnswer3_CN.Text))
                        {
                            answerList[2].Answer_CN = txtAnswer3_CN.Text;
                            answerList[2].Answer_EN = txtAnswer3_EN.Text;
                            AnswerPresent.Update(answerList[2]);
                        }
                        else
                        {
                            AnswerPresent.Delete(answerList[2].Id);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(txtAnswer3_CN.Text))
                        {
                            Model.Answer answer = new Model.Answer()
                            {
                                Answer_CN = txtAnswer3_CN.Text,
                                Answer_EN = txtAnswer3_EN.Text,
                                QuestionId = questionId
                            };
                            AnswerPresent.Create(answer);
                        }
                    }
                }
                else
                {
                    Model.Question newQuestion = new Model.Question()
                   {
                       Question_CN = txtQuestion_CN.Text,
                       Question_EN = txtQuestion_EN.Text,
                       Status = (int)QuestionStatus.Active
                   };
                    QusetionPresent.Create(newQuestion);
                    int newQuestionId = QusetionPresent.GetList(p => p.Status == (int)QuestionStatus.Active).LastOrDefault().QuestionId;
                    Model.Answer answer1 = new Model.Answer()
                    {
                        Answer_CN = txtAnswer1_CN.Text,
                        Answer_EN = txtAnswer1_EN.Text,
                        QuestionId = newQuestionId
                    };
                    AnswerPresent.Create(answer1);
                    Model.Answer answer2 = new Model.Answer()
                    {
                        Answer_CN = txtAnswer2_CN.Text,
                        Answer_EN = txtAnswer2_EN.Text,
                        QuestionId = newQuestionId
                    };
                    AnswerPresent.Create(answer2);
                    if (!string.IsNullOrEmpty(txtAnswer3_CN.Text))
                    {
                        Model.Answer answer3 = new Model.Answer()
                        {
                            Answer_CN = txtAnswer3_CN.Text,
                            Answer_EN = txtAnswer3_EN.Text,
                            QuestionId = newQuestionId
                        };
                        AnswerPresent.Create(answer3);
                    }
                }
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
            }
        }

        protected void lbtnReturn_Click(object sender, EventArgs e)
        {
            phQuestion.Visible = false;
            flag = false;
            lbtnStatus(flag);
        }
    }
}